/**
 * 
 */
package com.bms.web;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;

import com.bms.Logic;
import com.bms.abstracts.RESTController;

/**
 * @author IBM
 * 
 */
public class DownloadController extends RESTController {

	private Logic logic;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.bms.abstracts.RESTController#doGet(javax.servlet.http.HttpServletRequest
	 * , javax.servlet.http.HttpServletResponse)
	 */
	@Override
	protected ModelAndView doGet(HttpServletRequest request,
			HttpServletResponse response) {
		
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");

		String fileName = logic.codeing(request.getParameter("f"));
		String filePath = logic.ROOT_PATH() + fileName;

		response.setContentType("unknown");
		response.addHeader("Content-Disposition", "filename=\"" + fileName
				+ "\"");
		java.io.OutputStream os = null;
		java.io.FileInputStream fis = null;
		try {
			os = response.getOutputStream();
			fis = new java.io.FileInputStream(filePath);

			byte[] b = new byte[8192];
			int i = 0;

			while ((i = fis.read(b)) > 0) {
				os.write(b, 0, i);
			}
			os.flush();
		} catch (Exception e) {

		}
		
			return null;
			
			
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.bms.abstracts.RESTController#doPost(javax.servlet.http.HttpServletRequest
	 * , javax.servlet.http.HttpServletResponse)
	 */
	@Override
	protected ModelAndView doPost(HttpServletRequest request,
			HttpServletResponse response) {
		// TODO Auto-generated method stub
		return null;
	}

	public void setLogic(Logic logic) {
		this.logic = logic;
	}

}
